/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prs.web.rest.user;

import com.prs.util.PrsException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.prs.model.UserLevel;
import org.prs.persistence.UserLevelDatabasePersitenceManager;

/**
 *
 * @author richard
 */
public class UserTypeServlet extends HttpServlet {

    private static String GET_USER_TYPES="getusertypes";
    private UserLevelDatabasePersitenceManager manageUserLevel=new UserLevelDatabasePersitenceManager();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String call=request.getPathInfo().substring(1);
        JSONObject result=null;
        try {
            if(call.equals(GET_USER_TYPES)){
                result=new JSONObject();
                result.put("userlevels", getUserTypes());
            }
            if(result!=null){
                response.setContentType("appliation/json");
                result.write(out);
                out.close();
            }
        } catch(Exception e){
            throw  new PrsException(e);
        }finally {            
            out.close();
        }
    }
    private JSONArray getUserTypes(){
        JSONObject userTypeObject=new JSONObject();
        JSONArray userTypeObjects=new JSONArray();
        for(UserLevel userLevel:manageUserLevel.getAllUserLevels(0, manageUserLevel.getUserLevelCount(),"name")){
            userTypeObject.put("name", userLevel.getName());
            userTypeObject.put("description", userLevel.getDescription());
            userTypeObject.put("id", userLevel.getId());
            userTypeObjects.add(userTypeObject);
        }
        return  userTypeObjects;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
